﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Vision.Common;

namespace Vision.Utilities.CMS6UpdateTool
{
    public class AutoUpdateSettingsSection : ConfigurationSection
    {
        #region Const
        public const string SectionName = "autoUpdateSettings";

        const string ProcessorsPropertyName = "processors";
        const string ReleaseUpdaterTypePropertyName = "releaseUpdaterType";
        #endregion

        #region GetSettings
        public static AutoUpdateSettingsSection GetSettings(string configFilePath) 
        {
            if (File.Exists(configFilePath))
            {
                try
                {
                    var configSource = new FileConfigurationSource(configFilePath);
                    return configSource.GetSection(SectionName) as AutoUpdateSettingsSection;
                }
                catch { }
            }
            return null;
        }
        #endregion

        #region Ctor
        public AutoUpdateSettingsSection()
            : base()
        {
            ReleaseUpdaterTypeName = typeof(ReleaseUpdater).FullName;
            Processors = new NameTypeConfigurationElementCollection<GenericNameTypeData<IUpdateProcessor>, AutoUpdateProcessorConfigurationElement<IUpdateProcessor>>();
        }
        #endregion

        #region Properties
        [ConfigurationProperty(ReleaseUpdaterTypePropertyName)]
        public string ReleaseUpdaterTypeName
        {
            get { return (string)this[ReleaseUpdaterTypePropertyName]; }
            set { this[ReleaseUpdaterTypePropertyName] = value; }
        }

        [ConfigurationProperty(ProcessorsPropertyName)]
        public NameTypeConfigurationElementCollection<GenericNameTypeData<IUpdateProcessor>, AutoUpdateProcessorConfigurationElement<IUpdateProcessor>> Processors
        {
            get
            {
                return (NameTypeConfigurationElementCollection<GenericNameTypeData<IUpdateProcessor>, AutoUpdateProcessorConfigurationElement<IUpdateProcessor>>)this[ProcessorsPropertyName];
            }
            set
            {
                this[ProcessorsPropertyName] = value;
            }
        }
        #endregion

        #region CreateReleaseUpdater
        public virtual IReleaseUpdater CreateUpdater()
        {
            var result = default(IReleaseUpdater);
            var releaseUpdaterType = default(Type);
            if (!string.IsNullOrEmpty(ReleaseUpdaterTypeName))
            {
                try
                {
                    releaseUpdaterType = Type.GetType(ReleaseUpdaterTypeName);
                }
                catch { }
            }
            if (releaseUpdaterType != null && typeof(IReleaseUpdater).IsAssignableFrom(releaseUpdaterType))
            {
                result = (IReleaseUpdater)Activator.CreateInstance(releaseUpdaterType);
            }
            if (result == null)
                result = new ReleaseUpdater();

            return result;
        }
        #endregion
    }
}